<?php


namespace app\api\controller;


use app\api\model\CarModel;
use app\api\validate\CarValidate;
use app\common\controller\Api;

/**
 * 车辆信息
 * author: YJQ
 * Class Car
 * @package app\api\controller
 */
class Car extends Api
{

    protected $noNeedLogin = [];
    protected $noNeedRight = [];

    /**
     * 查看
     */
    public function index(CarModel $model,\app\admin\model\Driver $model_diver, CarValidate $validate)
    {

        $param = $this->request->param();
        //数据验证
        $validate_result = $validate->scene('index')->check($param);
        if (!$validate_result) {
            $this->error($validate->getError());
        }
        $user_id = $this->uid;
        //获取diver_id
        $driver_id = $model_diver->where('uid',$user_id)->value('id');
        if (!$driver_id) {
            $this->ajaxError('身份错误');
        }
        $info = $model->where('driver_id',$driver_id)->find();
        $data = [
            'info' => $info
        ];

        $this->ajaxSuccess($data);

    }

    /**
     * 更新
     */
    public function edit(CarModel $model,\app\admin\model\Driver $model_diver, CarValidate $validate)
    {

        $param = $this->request->param();
        //数据验证
        $validate_result = $validate->scene('edit')->check($param);
        if (!$validate_result) {
            $this->error($validate->getError());
        }
        $user_id = $this->uid;
        //获取diver_id
        $driver_id = $model_diver->where('uid',$user_id)->value('id');
        if (!$driver_id) {
            $this->ajaxError('身份错误');
        }
        //获取获取当前用户车辆信息
        $car = $model->where('driver_id',$driver_id)->find();

        unset($param['token']);

        if ($car->save($param)) {
            $this->ajaxSuccess();
        } else {
            $this->ajaxError('失败');
        }
    }
}
